<?php
namespace app\index\controller;
use think\Controller;
use think\Collection;
use think\Db;
class Circle extends CommonController
{
	public function circle(){

		$res= Db::table('page')->order('page_id desc')->select();
		$this ->assign('list',$res);
		return $this->fetch('circle');
	}
	public function circle_user_post(){
		// $res=Db::table('page')->where('page_id','data')->select();
		// var_dump(data);
		return $this->fetch('circle/circle_user_post');
	}
	public function circle_user_data($data){
		$res=Db::table('page')->alias('p')-> join('user u','p.user_id=u.user_id')->where('p.page_id',$data)->select();
		$comment=Db::table('comment')->alias('c')->join('user u','c.user_id=u.user_id')->where('c.page_id',$data)->select();
		// var_dump($comment);
		$this->assign('comment',$comment);			
		$this ->assign('list',$res);
		return $this->fetch('circle/circle_user_post');
	}
	public function addcomment($comment,$id,$page_id){
		$res = Db::table('comment')->insert(["comment_content"=>$comment,"user_id"=>getId($id),"page_id"=>$page_id,"comment_time"=>date("Y-m-d h:i:s")]);
		// var_dump($res.length);
		return $res;
	}
	public function user_name($id){
		$res = Db::table('user')->where('Number',$id)->select();
		return json($res);
	}
	//评论量
	public function comment_num($num){
		$res=Db::table('page')->alias('p')-> join('comment c','p.page_id=c.page_id')->where('p.page_id',$num)->select();
		Db::table('page')->where('page_id', $num)->update(['comment_num' => count($res)]);
		// $re=Db::table('page')->where('page_id',$data)->select();
	}
	//浏览量
	public function view_num($numb){
		$res=Db::table('page')->where('page_id',$numb)->select();
		// Db::table('page')->where('page_id', $data)->updata(['view_num'=>(int)$num]);
		$num=$res[0]['view_num'];
		// var_dump($num);
		Db::table('page')->where('page_id', $numb)->update(['view_num' => (int)$num+1]);
		return  dump($numb);
	}
	//小心心
	//$numb page_id 帖子唯一值
	//$sta 点赞状态
	//$user_id 当前用户账号
	public function content($numb, $sta, $user_name){
		$res=Db::table('page')->where('page_id',$numb)->select();
		$user_name=(int)$user_name;
		// $re =Db::table('page')->alias('p')->join('love l','p.page_id=c.page_id')->where('l.page_id',$numb)->select();
		//查询点赞状态
		$r =Db::table('love')->where('number',$user_name)->where('page_id',$numb)->select();
		if($r==null)
		Db::table('love')->insert(["number"=> $user_name,"page_id"=> $numb,"status"=>1]);		
		$num=$res[0]['content'];
		if($sta==1)
		{
			Db::table('page')->where('page_id', $numb)->update(['content' => (int)$num+1]);
			Db::table('love')->where('number', $user_name)->update(['status' => 1]);
		}
		else{
			Db::table('page')->where('page_id', $numb)->update(['content' => (int)$num-1]);
			Db::table('love')->where('number', $user_name)->update(['status' => 0]);
		}		
	}
	//是否点赞
	public function status($numb,$user_name){
		$res = Db::table('love')->where('page_id',$numb)->where('number',$user_name)->select();
		if($res==null)
		return 0;
		else
		$status=$res[0]['status'];
		return $status;
	}

}